package com.example.perplonews.a_d_fragment.b_perplo.fragment

import BaseFragment
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.example.perplonews.R
import com.example.perplonews.a_a_mvp.contract.News_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_a_mvp.model.models.News_Model
import com.example.perplonews.a_a_mvp.presenter.presenters.News_Presenter
import com.example.perplonews.a_b_entity.BaseEntity
import com.example.perplonews.a_b_entity.FollowAntivity
import com.example.perplonews.a_b_entity.greendao.UserEntity
import com.example.perplonews.a_b_utils.App
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.b_perplo.Per_News_Activity
import com.example.perplonews.a_d_fragment.b_perplo.adapter.Perplo_Rec_Adapter
import com.example.perplonews.a_d_fragment.b_perplo.adapter.Recom_Adapter
import com.example.perplonews.a_d_fragment.b_perplo.adapter.Top_Rec_Adapter
import com.example.perplonews.a_d_fragment.b_perplo.entity.UserLoveEntity
import com.example.perplonews.a_d_fragment.c_video.entity.Data
import com.example.perplonews.a_d_fragment.c_video.entity.SmallVideo
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class Love_Fragment : BaseFragment<News_Presenter>(), News_Contract.NewsView {

    private val loveRec: RecyclerView by lazy { mview.findViewById<RecyclerView>(R.id.love_rec) }
    private val loveLin: LinearLayout by lazy { findViewById<LinearLayout>(R.id.love_lin) }

    private var adapter2: Recom_Adapter? = null
    override fun findlayout(): Int {
        return R.layout.fragment_love_
    }

    override fun initView() {
        loveRec.layoutManager = LinearLayoutManager(context)
        loveRec.addItemDecoration(
            DividerItemDecoration(
                context,
                DividerItemDecoration.VERTICAL
            )
        )
    }

    override fun initData() {

        //适配关注列表
        //获取到id
//        var get = App.getDaoSession()?.queryBuilder(UserEntity::class.java)?.list()?.get(0)
//        var userId = get?.user_id
//        //调用网络请求
//        mypre = News_Presenter(News_Model(), this)
//        var list: ArrayList<HashMap<String, Any>> = ArrayList()
//        var hashMap: HashMap<String, Any> = HashMap()
//        hashMap.put("code", HttpCode.USERFOLLOWCODE)
//        hashMap.put("user_id", 1)
//        hashMap.put("page",1)
//        hashMap.put("code",HttpCode.FOLLOWNEWSCODE)
//        hashMap.put("page",1)
//        list.add(hashMap)
//
//        mypre.requestAll(list)


        //自己的
        OkGo.get<String>("http://v.juhe.cn/toutiao/index?type=top&key=e3080ff4a57ed090c999dc0d9082dce4")
            .execute(object :
                StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    var body = response.body()

                    var fromJson = Gson().fromJson(body, SmallVideo::class.java)

                        var data = fromJson.result?.data
                    if (data != null) {
                        LjlUtils.Ljl_Loge("=================")
                        var arrayList = data as ArrayList<Data>
                        var adapter = Top_Rec_Adapter(arrayList)


                        //设置下边rec
                        adapter2 = Recom_Adapter(arrayList)
                        loveRec.adapter = adapter2


                        // TODO: 2021/6/30 点击时间 点击进入到详情页，文字变为灰色   在实体类添加一个  boolean   两种状态分别加载两种不同的样式  这边改编状态后边 跟上 刷新适配器
                        //找到布局
                        var inflate = LayoutInflater.from(context).inflate(R.layout.love_head, null)
                        var headimg = inflate.findViewById<ImageView>(R.id.love_img)
                        var toprec = inflate.findViewById<RecyclerView>(R.id.love_rec_top)
                        //适配关注图片
                        Glide.with(context).load(R.mipmap.biao).into(headimg)
                        //布局样式
                        var linear = LinearLayoutManager(context)
                        linear.orientation = LinearLayoutManager.HORIZONTAL

                        toprec.adapter = adapter
                        toprec.layoutManager = linear
                        //线
                        toprec.addItemDecoration(
                            DividerItemDecoration(
                                context,
                                DividerItemDecoration.VERTICAL
                            )
                        )
                        // TODO: 2021/6/30 欠缺一个点击事件   点击进入到作者详情页

                        //添加头部剧
                        adapter2?.addHeaderView(inflate)
                    } else {
                        LjlUtils.Ljl_Loge("11111111")
                        // TODO: 2021/6/30 数据请求失败的时候 加载 一个占位符       正常应该用  请求数据那边的方法
                        var inflate =
                            LayoutInflater.from(context).inflate(R.layout.follow_error, null)

                        var text = inflate.findViewById<TextView>(R.id.error_tex)
                        //添加到页面上
                        loveRec.adapter = adapter2

                       adapter2?.addHeaderView(inflate)


                    }


                }

            })


    }

    override fun refreshAll(entity: BaseEntity) {
//        if (entity is UserLoveEntity){
//            //拿到数据集合
//            var userlove = entity as UserLoveEntity
//            var values = userlove.getValues()
//            var adapter = Top_Rec_Adapter(values as ArrayList<UserLoveEntity.Values>)
//
//            loveRecTop.adapter = adapter
//
//            var linear = LinearLayoutManager(context)
//            linear.orientation = LinearLayoutManager.HORIZONTAL
//            loveRecTop.layoutManager = linear
//
//        }
//        if (entity is FollowAntivity){
//            var follow = entity as FollowAntivity
//            var values = follow.getValues()
//
//        }
    }

    override fun startPage(bundle: Bundle, clazz: Class<Any>) {

    }


}